var Shengchanqiye = new Vue({
    el: "#Shengchanqiyelist",
    data: {
        xiangmuId: $("#xiangmuId").val(),
        xiangmuLeixing: $("#xiangmuLeixing").val(),
        xiangmu: {},
        shengchanqiye: {},
        shengchanqiyelist: [],
        show: false,					// 显示列表是否有数据

        pinguiList: [],
        pinpaiList: [],
        pinzhongList: [],
        shengchanqiye: {},
        id: '',

        pageIndex: 1,
        pageSize: 9999,
        pageCount: 0,
        recordCount: 0,
        inputPageIndexValue: "",
        getCount: 1,
        passcount: 0,
        totalcount: 0,
        needcount: 0,
        keshi: "",
        lunci: 0,

    },
    computed: {
        getCounttext: function () {
            if(this.getCount == 1){
                return "品规"
            }else if(this.getCount == 2){
                return "品牌"
            }else if(this.getCount == 3){
                return "品种"
            }
        }
    },

    created: function () {
        window.creatTitle = this.creatTitle;
        window.creatBody = this.creatBody;
        this.findIsacticed()
    },
    methods: {
        toback: function () {
            window.history.go(-1);
        },
        findIsacticed: function () {
            var _this = this;
            layer.open({type: 3});
            $.post('/xiangmu/find_one', {
                xianmguid: _this.xiangmuId,
                rdm: Math.random()
            }, function (ppData) {
                layer.closeAll("loading");
                if (ppData != null) {
                    if (ppData.result == "1") {
                        var data = ppData.resultContent;
                        _this.xiangmu = data;
                        _this.xiangmuId = _this.xiangmu.xiangmuid;
                        _this.xiangmuleixing = _this.xiangmu.xiangmuleixing;
                        _this.usernum = data.zhuangjiashu;
                        _this.bindList()
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },


        bindList: function () {
            var _this = this;
            layer.open({type: 3});
            $.post("/TyController/bindHuizongbiaoviewList", {
                xiangmuid: _this.xiangmuId,
                xiangmuleixing: _this.xiangmuLeixing,
                pageindex: _this.pageIndex,
                pagesize: _this.pageSize,
                rdm: Math.random()
            }, function (ppData) {
                layer.closeAll("loading");
                if (ppData != null) {
                    if (ppData.result == "1") {
                        var data = ppData.resultContent;
                        _this.getCount = data.getCount;
                        _this.passcount = data.passcount;
                        _this.totalcount = data.totalcount;
                        _this.needcount = data.needcount;
                        _this.keshi = data.keshi;
                        _this.lunci = data.lunci;
                        window.lunci = data.lunci;
                        var mmHtml = ""
                        switch (data.Xunhuan) {
                            case 1:
                                mmHtml += _this.createHtml1(data, _this.xiangmuleixing);
                                break;
                            case 2:
                                mmHtml += _this.createHtml2(data, _this.xiangmuleixing);
                                break;
                            case 3:
                                mmHtml += _this.createHtml3(data, _this.xiangmuleixing);
                                break;
                        }
                        $("#List").html(mmHtml);
                    } else {
                        layer.alert(ppData.message);
                    }
                }

            }, "json");
        },

        createHtml1: function (data, xiangmuLeixing) {
            var _this = this
            {
                var mmHtml = ""
                mmHtml += createTitle(xiangmuLeixing,2)

                if (data.pinguiList.length > 0) {
                    _this.show = true;
                    _this.pinguiList = data.pinguiList;

                    var PageInfo = data.PageInfo;
                    _this.pageIndex = PageInfo.pageIndex;
                    _this.recordCount = PageInfo.recordCount;
                    _this.pageCount = PageInfo.pageCount;


                    for (var i = 0; i < _this.pinguiList.length; i++) {
                        var mmpinguilist = _this.pinguiList[i]
                        var mmId = mmpinguilist.id
                        var text1 = mmpinguilist.text1
                        var text2 = mmpinguilist.text2
                        var text3 = mmpinguilist.text3
                        var text4 = mmpinguilist.text4
                        var text5 = mmpinguilist.text5
                        var text6 = mmpinguilist.text6
                        var text7 = mmpinguilist.text7
                        var text8 = mmpinguilist.text8
                        var text9 = mmpinguilist.text9
                        var text10 = mmpinguilist.text10
                        var text11 = mmpinguilist.text11
                        var text12 = mmpinguilist.text12
                        var text13 = mmpinguilist.text13
                        var text14 = mmpinguilist.text14
                        var text15 = mmpinguilist.text15
                        var text16 = mmpinguilist.text16
                        var text17 = mmpinguilist.text17
                        var text18 = mmpinguilist.text18
                        var text19 = mmpinguilist.text19
                        var text20 = mmpinguilist.text20
                        var mmResult = mmpinguilist.result
                        var isvote = mmpinguilist.isvote
                        var ordernumber = mmpinguilist.ordernumber
                        var mmPiaoshu = mmpinguilist.piaoshu


                        var mmPinguixuhao = 1
                        var mmHuanhangfu = "<tr>";

                        mmHtml += createBody1(xiangmuLeixing, mmpinguilist)

                        mmHtml += "<td style='text-align:center;vertical-align:middle;width:100px;'>" + mmPiaoshu + "</td>";
                        if (mmResult == "通过") mmHtml += "<td style='text-align:center;vertical-align:middle;background: #0080004a;width:100px;'>" + mmResult + "</td>";
                        if (mmResult == "不通过") mmHtml += "<td style='text-align:center;vertical-align:middle;background: #ff000096;width:100px;'>" + mmResult + "</td>";
                        mmHtml += "</tr>";
                    }

                }
            }
            return mmHtml += "</tbody></table>";


        },
        createHtml2: function (data, xiangmuLeixing) {
            var _this = this
            {
                var mmHtml = ""
                mmHtml += createTitle(xiangmuLeixing,2)

                if (data.pinpaiList.length > 0) {
                    _this.show = true;
                    _this.pinpaiList = data.pinpaiList;

                    var PageInfo = data.PageInfo;
                    _this.pageIndex = PageInfo.pageIndex;
                    _this.recordCount = PageInfo.recordCount;
                    _this.pageCount = PageInfo.pageCount;

                    for (var i = 0; i < _this.pinpaiList.length; i++) {
                        var pinpaiONE = _this.pinpaiList[i]
                        var pinguiList = pinpaiONE.pinguiList
                        var mmPinpaiXuhao = pinpaiONE[data.pinpaixuhao];
                        var mmPinguiHebingshu = pinpaiONE.pinguihebinghang;

                        for (var j = 0; j < pinguiList.length; j++) {
                            var mmpinguiONE = pinguiList[j]
                            var mmId = mmpinguiONE.id
                            var text1 = mmpinguiONE.text1
                            var text2 = mmpinguiONE.text2
                            var text3 = mmpinguiONE.text3
                            var text4 = mmpinguiONE.text4
                            var text5 = mmpinguiONE.text5
                            var text6 = mmpinguiONE.text6
                            var text7 = mmpinguiONE.text7
                            var text8 = mmpinguiONE.text8
                            var text9 = mmpinguiONE.text9
                            var text10 = mmpinguiONE.text10
                            var text11 = mmpinguiONE.text11
                            var text12 = mmpinguiONE.text12
                            var text13 = mmpinguiONE.text13
                            var text14 = mmpinguiONE.text14
                            var text15 = mmpinguiONE.text15
                            var text16 = mmpinguiONE.text16
                            var text17 = mmpinguiONE.text17
                            var text18 = mmpinguiONE.text18
                            var text19 = mmpinguiONE.text19
                            var text20 = mmpinguiONE.text20
                            var mmResult = mmpinguiONE.result
                            var isvote = mmpinguiONE.isvote
                            var ordernumber = mmpinguiONE.ordernumber
                            var mmPiaoshu = mmpinguiONE.piaoshu

                            var mmPinguixuhao = 1
                            var mmHuanhangfu = "";

                            var mmBtndiv = "";
                            var mmerjihe = "";
                            mmHtml += createBody2(xiangmuLeixing, j, mmpinguiONE, mmPinguiHebingshu)
                            if (IsVotePinpai(xiangmuLeixing)) {
                                if (j != 0) {
                                    mmHuanhangfu = "<tr>";
                                } else {
                                    mmerjihe = ""
                                    mmerjihe += "<td style='text-align:center;vertical-align:middle;height: 100%;width:100px;'  rowspan='" + mmPinguiHebingshu + "'>" + mmPiaoshu + "</td>";
                                    if (mmResult == "通过") mmerjihe += "<td style='text-align:center;vertical-align:middle;background: #0080004a;height: 100%;width:100px;' rowspan='" + mmPinguiHebingshu + "'>" + mmResult + "</td>";
                                    if (mmResult == "不通过") mmerjihe += "<td style='text-align:center;vertical-align:middle;background: #ff000096;height: 100%;width:100px;' rowspan='" + mmPinguiHebingshu + "'>" + mmResult + "</td>";
                                    if (mmResult == "") mmerjihe += "<td style='text-align:center;vertical-align:middle;height: 100%;width:100px;' rowspan='" + mmPinguiHebingshu + "'>" + mmResult + "</td>";

                                }
                                mmHtml += mmerjihe
                            } else {
                                mmHtml += "<td style='text-align:center;vertical-align:middle;width:100px;'>" + mmPiaoshu + "</td>";
                                if (mmResult == "通过") mmHtml += "<td style='text-align:center;vertical-align:middle;background: #0080004a;width:100px;'>" + mmResult + "</td>";
                                if (mmResult == "不通过") mmHtml += "<td style='text-align:center;vertical-align:middle;background: #ff000096;width:100px;'>" + mmResult + "</td>";
                                if (mmResult == "") mmHtml += "<td style='text-align:center;vertical-align:middle;width:100px;'>" + mmResult + "</td>";
                            }
                            mmHtml += "</tr>";
                        }
                    }

                }
            }
            return mmHtml += "</tbody></table>";


        },
        createHtml3: function (data, xiangmuLeixing) {
            var _this = this;
            var mmHtml = "";
            mmHtml += createTitle(xiangmuLeixing, 2)

            if (data.pinzhongList && data.pinzhongList.length > 0) {
                _this.show = true;
                _this.pinzhongList = data.pinzhongList;

                var PageInfo = data.PageInfo;
                _this.pageIndex = PageInfo.pageIndex;
                _this.recordCount = PageInfo.recordCount;
                _this.pageCount = PageInfo.pageCount;

                // 获取后端返回的列索引
                var pinpaixuhao = data.pinpaixuhao;
                var pinzhongxuhao = data.pinzhongxuhao;

                // 预处理计算每个品种的总行数
                for (var i = 0; i < _this.pinzhongList.length; i++) {
                    var pinzhongONE = _this.pinzhongList[i];
                    var totalRows = 0;

                    // 计算该品种下所有品牌的品规总数
                    for (var j = 0; j < pinzhongONE.pinpaiList.length; j++) {
                        var pinpaiONE = pinzhongONE.pinpaiList[j];
                        totalRows += pinpaiONE.pinguiList.length;
                    }

                    // 更新品种的合并行数
                    pinzhongONE.pinguihebinghang = totalRows;
                }
                
                // 品种颜色设置 - 奇数偶数两种颜色
                var pinzhongColorOdd = "lightblue";  // 品种奇数行颜色
                var pinzhongColorEven = "#ffffff"; // 品种偶数行颜色
                
                // 添加品牌计数器，用于隔行换色
                var brandColorCounter = 0;
                // 存储品牌颜色的数组
                var brandColors = [];

                for (var i = 0; i < _this.pinzhongList.length; i++) {
                    var pinzhongONE = _this.pinzhongList[i];
                    var pinpaiList = pinzhongONE.pinpaiList;
                    var mmPinzhongXuhao = pinzhongONE.pinzhongone ? pinzhongONE.pinzhongone[pinzhongxuhao] : "";
                    var mmPinzhongHebingshu = pinzhongONE.pinguihebinghang;
                    
                    // 根据品种索引确定颜色
                    var pinzhongColor = (i % 2 === 0) ? pinzhongColorOdd : pinzhongColorEven;
                    
                    // 设置全局变量，供createBody3函数使用
                    window.currentPinzhongColor = pinzhongColor;

                    for (var j = 0; j < pinpaiList.length; j++) {
                        var pinpaiONE = pinpaiList[j];
                        var pinguiList = pinpaiONE.pinguiList;
                        var mmPinpaiXuhao = pinpaiONE.pinpaione ? pinpaiONE.pinpaione[pinpaixuhao] : "";
                        var mmPinguiHebingshu = pinpaiONE.pinguihebinghang;
                        
                        // 每次切换品牌时，增加计数器，用于隔行换色
                        brandColorCounter++;
                        // 确定当前品牌行是否需要着色
                        var needColoring = (brandColorCounter % 2 === 1);

                        // 为每个需要着色的品牌设置颜色
                        if (needColoring) {
                            brandColors[j] = "lightblue";
                        } else {
                            brandColors[j] = "#ffffff";
                        }

                        for (var k = 0; k < pinguiList.length; k++) {
                            var mmpinguiONE = pinguiList[k];
                            var mmId = mmpinguiONE.id;
                            var mmResult = mmpinguiONE.result;
                            var mmPinguixuhao = mmpinguiONE.id;
                            var mmPiaoshu = mmpinguiONE.piaoshu;
                            
                            // 判断是否是品种的第一行和品牌的第一行
                            var isFirstPinzhong = (j === 0 && k === 0);
                            var isFirstPinpai = (k === 0);
                            
                            // 使用品牌对应的颜色
                            var trStyle = ' style="background-color:' + brandColors[j] + ';"';
                            // mmHtml += "<tr" + trStyle + ">";
                            mmHtml += "<tr>";

                            // 使用createBody3函数来创建表格行内容
                            mmHtml += createBody3(xiangmuLeixing, k, j, mmpinguiONE, mmPinguiHebingshu, mmPinzhongHebingshu, isFirstPinpai, isFirstPinzhong);
                            
                            // 处理不同投票模式下的票数和结果显示
                            var mmerjihe = "";
                            
                            // 处理品种投票模式
                            if (IsVotePinzhong(xiangmuLeixing)) {
                                if (isFirstPinzhong) {
                                    mmerjihe = `<td style='text-align:center;vertical-align:middle;width:100px;' rowspan='${mmPinzhongHebingshu}'>${mmPiaoshu}</td>`;
                                    if (mmResult == "通过") mmerjihe += `<td style='text-align:center;vertical-align:middle;background: #0080004a;width:100px;' rowspan='${mmPinzhongHebingshu}'>${mmResult}</td>`;
                                    if (mmResult == "不通过") mmerjihe += `<td style='text-align:center;vertical-align:middle;background: #ff000096;width:100px;' rowspan='${mmPinzhongHebingshu}'>${mmResult}</td>`;
                                    if (mmResult == "") mmerjihe += `<td style='text-align:center;vertical-align:middle;width:100px;' rowspan='${mmPinzhongHebingshu}'>${mmResult}</td>`;
                                    mmHtml += mmerjihe;
                                }
                            }
                            // 处理品牌投票模式
                            else if (IsVotePinpai(xiangmuLeixing)) {
                                if (isFirstPinpai) {
                                    mmerjihe = `<td style='text-align:center;vertical-align:middle;width:100px;' rowspan='${mmPinguiHebingshu}'>${mmPiaoshu}</td>`;
                                    if (mmResult == "通过") mmerjihe += `<td style='text-align:center;vertical-align:middle;background: #0080004a;width:100px;' rowspan='${mmPinguiHebingshu}'>${mmResult}</td>`;
                                    if (mmResult == "不通过") mmerjihe += `<td style='text-align:center;vertical-align:middle;background: #ff000096;width:100px;' rowspan='${mmPinguiHebingshu}'>${mmResult}</td>`;
                                    if (mmResult == "") mmerjihe += `<td style='text-align:center;vertical-align:middle;width:100px;' rowspan='${mmPinguiHebingshu}'>${mmResult}</td>`;
                                    mmHtml += mmerjihe;
                                }
                            }
                            // 处理品规投票模式（默认）
                            else {
                                mmHtml += `<td style='text-align:center;vertical-align:middle;width:100px;'>${mmPiaoshu}</td>`;
                                if (mmResult == "通过") mmHtml += `<td style='text-align:center;vertical-align:middle;background: #0080004a;width:100px;'>${mmResult}</td>`;
                                if (mmResult == "不通过") mmHtml += `<td style='text-align:center;vertical-align:middle;background: #ff000096;width:100px;'>${mmResult}</td>`;
                                if (mmResult == "") mmHtml += `<td style='text-align:center;vertical-align:middle;width:100px;'>${mmResult}</td>`;
                            }
                            mmHtml += "</tr>";
                        }
                    }
                }
            }

            return mmHtml + "</tbody></table></div>";
        },

        //跳到首页
        SetPageIndex: function () {
            this.pageIndex = 1;
        },
        SetPageEnd: function () {
            this.pageIndex = this.pageCount;
        },
        //上一页
        SetPageIndexPrePage: function () {
            var PrePage = ((this.pageIndex - 1) <= 0) ? 1 : (this.pageIndex - 1);
            this.pageIndex = PrePage;
        },
        //下一页
        SetPageIndexNextPage: function () {
            var NextPage = ((this.pageIndex + 1) >= this.pageCount) ? this.pageCount : (this.pageIndex + 1);
            this.pageIndex = NextPage;
        },
        //跳转界面
        JumpPage: function () {
            if (this.inputPageIndexValue <= 1) {
                this.inputPageIndexValue = 1;
            } else if (this.inputPageIndexValue >= this.pageCount) {
                this.inputPageIndexValue = this.pageCount;
            }
            this.pageIndex = this.inputPageIndexValue;
            this.inputPageIndexValue = '';
        },

    },
    watch: {
        //监控分页情况，刷新列表
        pageIndex: function () {

            this.bindList();
        }
    }
});
